﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AvalonDock;
using RiskMan.DataClass.Models.Sterling;
using System.Windows.Threading;
using RiskMan.Views.Dialogs;
using TheControllerRisk.Settings;
using RiskMan.DataClass.Models.NotBasModels;

namespace RiskMan.Views.Streling
{
    /// <summary>
    /// Interaction logic for SterlingRobot.xaml
    /// </summary>

    public partial class SterlingRobot : DocumentContent
    {
        private MainWindow mainWindow;
        private DispatcherTimer _dsTimer = new DispatcherTimer();

        public SterlingRobot()
        {
            mainWindow = MainWindow.StaticLevel;

            InitializeComponent();

            dtgdSterling.ItemsSource = mainWindow.DataProvider.SterlingItemList;
        }

        private void SterlingRobot_OnLoaded (object sender, RoutedEventArgs e)
        {
            _dsTimer.Interval = TimeSpan.FromMilliseconds(1000);
            _dsTimer.Start();
            _dsTimer.Tick += new EventHandler(_dsTimer_Tick);
        }

        public void _dsTimer_Tick(Object sender, EventArgs args)
        {
            bool running = false;

            running = mainWindow.ConnectingOrDisconnecting(running);

            if (running == true)
            {
                if (mainWindow.DataProvider.SterlingItemList.Count > 0)
                {
                    foreach (var item in mainWindow.DataProvider.SterlingItemList)
                    {
                        if (item.IsRun == true)
                        {
                            SterlingLogik logik = new SterlingLogik(mainWindow, item);
                            logik.Start(item);
                        }
                    }

                }
            }
        }

        private void Button_AddRow_Click(object sender, RoutedEventArgs e)
        {
            SterlingItem item = new SterlingItem();
            AddSterlingRobot add = new AddSterlingRobot(item, mainWindow.DataProvider);
            add.ShowDialog();

            if (add.DialogResult == true)
            {
                mainWindow.DataProvider.SterlingItemList.Add(item);
            }
        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            if (dtgdSterling.SelectedItems.Count == 1)
            {
                int index = dtgdSterling.SelectedIndex + 1;
                //dtgdLevel.UnselectAll();
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите скопировать строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow.DataProvider.SterlingItemList)
                        {
                            if (item.GuidRobot == ((SterlingItem)(dtgdSterling.SelectedItem)).GuidRobot)
                            {
                                var tmp = (SterlingItem)cfg.DeepClone(item);
                                tmp.IsRun = false;
                                tmp.GuidRobot= Guid.NewGuid();
                                tmp.TradeList = new System.Collections.ObjectModel.ObservableCollection<TradesOpen>();
                                mainWindow.DataProvider.SterlingItemList.Add(tmp);
                            }
                        }

                    }
                }
                catch
                { }
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dtgdSterling.SelectedItems.Count == 1)
            {
                int index = dtgdSterling.SelectedIndex + 1;
                //dtgdLevel.UnselectAll();
                try
                {
                    if (new DialogOkCancel("Вы действительно хотите удалить строку " + index + "?", "Внимание").Result == MessageBoxResult.OK)
                    {
                        foreach (var item in mainWindow.DataProvider.SterlingItemList)
                        {
                            if (item.GuidRobot == ((SterlingItem)(dtgdSterling.SelectedItem)).GuidRobot)
                            {
                                mainWindow.DataProvider.SterlingItemList.Remove(item);
                                break;
                            }
                        }

                    }
                }
                catch
                { }
            }
        }

        private void dtgdSterling_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (dtgdSterling.SelectedItems.Count == 1)
            {
                int index = dtgdSterling.SelectedIndex + 1;
                //dtgdLevel.UnselectAll();
                try
                {
                        foreach (var item in mainWindow.DataProvider.SterlingItemList)
                        {
                            if (item.GuidRobot == ((SterlingItem)(dtgdSterling.SelectedItem)).GuidRobot)
                            {
                                AddSterlingRobot add = new AddSterlingRobot(item, mainWindow.DataProvider);
                                add.ShowDialog();
                                break;
                            }
                        }

                }
                catch
                { }
            }
        }


        private void dtgdSterling_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
